package com.bgrconsulting.stockorderentry.service.rest.impl;

import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;


import com.bgrconsulting.stockorderentry.service.converter.response.AccountPositionResponseConverter;
import com.bgrconsulting.stockorderentry.service.dao.AccountBalanceResponseDao;
import com.bgrconsulting.stockorderentry.service.dao.AccountPositionResponseDao;
import com.bgrconsulting.stockorderentry.service.dao.AccountResponseDao;


import com.bgrconsulting.stockorderentry.service.model.Account;
import com.bgrconsulting.stockorderentry.service.model.AccountBalance;
import com.bgrconsulting.stockorderentry.service.model.CashAccountBalance;
import com.bgrconsulting.stockorderentry.service.model.DTBalance;
import com.bgrconsulting.stockorderentry.service.model.MarginAccountBalance;
import com.bgrconsulting.stockorderentry.service.model.AccountPosition;
import com.bgrconsulting.stockorderentry.service.model.AccountPositions;
import com.bgrconsulting.stockorderentry.service.model.Product;
import com.bgrconsulting.stockorderentry.service.model.response.AccountBalanceResponse;
import com.bgrconsulting.stockorderentry.service.model.response.AccountPositionResponse;


@Path("/accounts/rest/accountposition/")
public class AccountPositionResource {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	String id;
	
	
	@GET
	 @Produces({"application/xml", "application/json"})
	 @Path("/{accountId}") 
	 public AccountPositionResponseConverter getAccount(@PathParam ("accountId") String accountId) {
		
		//TODO: Make this into an external call to a backend system.
		Account ac = new Account("INDIVUAL", accountId, "MARGIN", "", 657.37, "INDIVIDUAL");
		AccountBalance ab = new AccountBalance();
		ab.setCashAvailableForWithDrawal(0.00);
		ab.setNetAccountValue(-663.20);
		ab.setNetCash(740.450013);
		ab.setSweepDepositAmount(0.00);
		ab.setTotalLongValue(122.23);
		ab.setTotalSecurituesMktValue(-454.22);
		
		DTBalance dtb = new DTBalance();
		dtb.setDTCashBalance(0.00);
		dtb.setDTMarginBalance(0.00);
		dtb.setDTMarginSecurities(-4183.79);
		dtb.setDTNonMarginableSecuritiesAndOptions(-2714.88);
		dtb.setDTStatus("QIALIFIED_4_X");
		
		MarginAccountBalance mab = new MarginAccountBalance();
		
		mab.setDTBalance(dtb);
		mab.setFedCall(-105.49);
		mab.setMarginBalance(-740.45);
		mab.setMarginBalanceWithDrawal(-2714.88);
		mab.setMarginEquity(-714.88);
		mab.setMarginableSecurities(-4183.73);
		mab.setMaxAvailableForWithDrawal(-2714.88);
		mab.setMinEquityCall(-25663.20);
		mab.setNonMarginableSecuritiesAndOptions(-2714.88);
		
		
		CashAccountBalance cab = new CashAccountBalance();
		
		cab.setCashAvailableForInvestment(1000.23);
		cab.setCashBalance(203432.23);
		cab.setSettledCashForInvestment(12323.12);
		
		
		AccountPosition[] accountPosition = new AccountPosition[8];
		
		accountPosition[0] = new AccountPosition();
		accountPosition[0].setCostBasis(100.00);
		accountPosition[0].setLongOrShort("");
		accountPosition[0].setMarketValue(100.00);
		accountPosition[0].setMarginable(false);
		
		Product productId = new Product();
		productId.setMarket("TSE");
		productId.setSymbol("RIM");
		productId.setTypeCode("000");
		
		accountPosition[0].setProduct(productId);
		accountPosition[0].setQuanity(100.00);
		
		accountPosition[1] = new AccountPosition();
		accountPosition[1].setCostBasis(110.00);
		accountPosition[1].setLongOrShort("SHORT");
		accountPosition[1].setMarketValue(110.00);
		accountPosition[1].setMarginable(false);
		
		productId = new Product();
		productId.setMarket("TSE");
		productId.setSymbol("BNS");
		productId.setTypeCode("000");
		
		accountPosition[1].setProduct(productId);
		accountPosition[1].setQuanity(100.00);
		
		accountPosition[2] = new AccountPosition();
		accountPosition[2].setCostBasis(100.00);
		accountPosition[2].setLongOrShort("");
		accountPosition[2].setMarketValue(100.00);
		accountPosition[2].setMarginable(false);
		
		productId = new Product();
		productId.setMarket("NYSE");
		productId.setSymbol("WMT");
		productId.setTypeCode("000");
		
		accountPosition[2].setProduct(productId);
		accountPosition[2].setQuanity(100.00);
		
		accountPosition[3] = new AccountPosition();
		accountPosition[3].setCostBasis(110.00);
		accountPosition[3].setLongOrShort("SHORT");
		accountPosition[3].setMarketValue(110.00);
		accountPosition[3].setMarginable(false);
		
		productId = new Product();
		productId.setMarket("TSE");
		productId.setSymbol("BMO");
		productId.setTypeCode("000");
		
		accountPosition[3].setProduct(productId);
		accountPosition[3].setQuanity(100.00);
		
		
		accountPosition[4] = new AccountPosition();
		accountPosition[4].setCostBasis(100.00);
		accountPosition[4].setLongOrShort("");
		accountPosition[4].setMarketValue(100.00);
		accountPosition[4].setMarginable(false);
		
		productId = new Product();
		productId.setMarket("TSE");
		productId.setSymbol("BCE");
		productId.setTypeCode("000");
		
		accountPosition[4].setProduct(productId);
		accountPosition[4].setQuanity(100.00);
		
		accountPosition[5] = new AccountPosition();
		accountPosition[5].setCostBasis(110.00);
		accountPosition[5].setLongOrShort("SHORT");
		accountPosition[5].setMarketValue(110.00);
		accountPosition[5].setMarginable(false);
		
		productId = new Product();
		productId.setMarket("NYSE");
		productId.setSymbol("BMY");
		productId.setTypeCode("000");
		
		accountPosition[5].setProduct(productId);
		accountPosition[5].setQuanity(100.00);
		
		accountPosition[6] = new AccountPosition();
		accountPosition[6].setCostBasis(100.00);
		accountPosition[6].setLongOrShort("");
		accountPosition[6].setMarketValue(100.00);
		accountPosition[6].setMarginable(false);
		
		productId = new Product();
		productId.setMarket("NYSE");
		productId.setSymbol("DSGX");
		productId.setTypeCode("000");
		
		accountPosition[6].setProduct(productId);
		accountPosition[6].setQuanity(100.00);
		
		accountPosition[7] = new AccountPosition();
		accountPosition[7].setCostBasis(110.00);
		accountPosition[7].setLongOrShort("SHORT");
		accountPosition[7].setMarketValue(110.00);
		accountPosition[7].setMarginable(false);
		
		productId = new Product();
		productId.setMarket("NYSE");
		productId.setSymbol("ZMH");
		productId.setTypeCode("000");
		
		accountPosition[7].setProduct(productId);
		accountPosition[7].setQuanity(100.00);
		
		AccountPositions accountPositions = new AccountPositions(accountPosition);
		
		
		AccountPositionResponse apr = new AccountPositionResponse(ac, accountPosition.length, "", accountPositions);
		
		AccountPositionResponseConverter aprc = new AccountPositionResponseConverter(apr);
		
		return aprc;
		
	
	}
	
	@PUT
	@Consumes({"application/xml","application/json"})
	public Response createNewAccountBalance (JAXBElement <AccountPosition> accountPositionResponse) {
		
		
		return putAndGetResponse(accountPositionResponse.getValue());
		
    }
	
	
	
	private Response putAndGetResponse(AccountPosition accountPositionResponse) {
		Response res;
		if(AccountPositionResponseDao.instance.getModel().containsKey(accountPositionResponse.getAccountId())) {
			res = Response.noContent().build();
		} else {
			res = Response.created(uriInfo.getAbsolutePath()).build();
		}
		AccountPositionResponseDao.instance.getModel().put(accountPositionResponse.getAccountId(), accountPositionResponse);
		return res;
	}
}
